package nameservice;

import nameservice.connect.ConnectDialog;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class NameServiceGUI extends javax.swing.JFrame {

    private static Logger logger;
    DefaultTableModel model;
    private final ConnectDialog connectDialog = new nameservice.connect.ConnectDialog(this, true);

    public NameServiceGUI() {
        logger = getLogger();
        connectDialog.setVisible(true);
        initComponents();
        model = (DefaultTableModel) jTable1.getModel();
        center(this);
        new NameServiceCore(connectDialog.getPort(), model).execute();
    }

    private void center(Component c) {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int w = c.getSize().width;
        int h = c.getSize().height;
        c.setLocation((dim.width - w) / 2, (dim.height - h) / 2);
    }

    @SuppressWarnings({ "unchecked" })
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("NameService");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "host", "port", "name", "typ"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 751, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NameServiceGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    /**
     * Creates a logger.
     *
     * @return
     */
    public static Logger getLogger() {
        FileHandler fhandler = null;
        if (logger == null) {
            try {
                fhandler = new FileHandler("NameService.log");
                fhandler.setFormatter(new SimpleFormatter());
                logger = Logger.getLogger("NameService");
                logger.addHandler(fhandler);
                logger.setLevel(Level.INFO);
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return logger;
    }
}
